package com.study.zzedu.ui.noticeinfodetail;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.entity.YiDongModel;
import com.study.zzedu.utils.DeviceUtil;
import com.study.zzedu.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

public class NoticeInfoDetailActivity extends BaseMvpActivity<BaseView, NoticeInfoDetailPresenter> implements BaseView {

    private TextView tvTime, tvTitle, tvCreator;
    private WebView wv_info;
    StringBuffer sb = new StringBuffer();

    private RelativeLayout btn_back;
    @Override
    protected int getLayoutID() {
        return R.layout.activity_notice_info_detail;
    }

    @Override
    protected NoticeInfoDetailPresenter createPresenter() {
        return new NoticeInfoDetailPresenter();
    }

    @Override
    protected BaseView createView() {
        return this;
    }

    @Override
    protected void initView() {
        btn_back = findViewById(R.id.rl_back);
        tvCreator = findViewById(R.id.tv_detail_creator);
        tvTime = findViewById(R.id.tv_detail_time);
        tvTitle = findViewById(R.id.tv_detail_title);
        wv_info = findViewById(R.id.wv_info);
    }

    @Override
    protected void initData() {
        String id = getIntent().getStringExtra("infoID");
        String from = getIntent().getStringExtra("from");
        getPresenter().getNoticeDetail(NoticeInfoDetailActivity.this, id, from);
    }

    @Override
    protected void initListener() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void getData(String data) {
        String content = "";
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jArray = jObject.getJSONArray("data");
            if (jArray.length() > 0) {
                tvCreator.setText(jArray.getJSONObject(0).getString("creator"));
                tvTitle.setText(jArray.getJSONObject(0).getString("title"));
                tvTime.setText(jArray.getJSONObject(0).getString("createDate"));
                content = jArray.getJSONObject(0).getString("content");
            } else {
                return;
            }
        } catch (Exception e) {
            e.toString();
        }
        //int screenWidth = DeviceUtil.deviceWidth(NoticeInfoDetailActivity.this);
        //content = content.replace("<img", "<img style=\"max-width:"+(screenWidth/2)+"px\"");
        //content = content.replace("<p", "<p style=\"font-size:30px\"");
        //content = content.replace("<img","<img width=\"100px\"");
        //添加html
        //添加文件的内容
        sb.append("<html><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1.0'><style>video,img{max-width: 100% !important;height: auto !important;}</style></head><body style='color: black'><p></p>");
//        content = content.replace("<p", "<p style=\"font-size:30px\"");
//        //content = content.replace("<img","<img width=\"100px\"");
//        //添加html
//        sb.append("<html><head><meta http-equiv='content-type' content='text/html; charset=utf-8'>");
//        sb.append("<meta charset='utf-8'  content='1'></head><body style='color: black'><p></p>");

        sb.append(content);
        //加载本地文件
        // sb.append("<img src='file:///"+AContext.getFileUtil().getDownloadsPath()+"'>");
        sb.append("</body></html>");
        // webView.loadData(data, mimeType, encoding);
        //设置字符编码，避免乱码
        wv_info.getSettings().setDefaultTextEncodingName("utf-8");
        //支持javascript
        wv_info.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        wv_info.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        //wv_info.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        wv_info.getSettings().setUseWideViewPort(true);
        //自适应屏幕
        wv_info.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        wv_info.getSettings().setLoadWithOverviewMode(true);
        wv_info.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", null);
    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }
}